﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessService;
using CognisessDA;
using CognisessCommLib;
using Webdiyer.WebControls.Mvc;

namespace CognisessWeb.Controllers
{
    public class FeedBackController : Controller
    {
        AccountBLL accountBLL = new AccountBLL();

        #region Get

        // GET: /FeedBack/
        public ActionResult Index(int? id)
        {
            PagedList<Feedback> result = accountBLL.GetPagingFeedback(new Feedback()).ToPagedList(id ?? 1, 10);
            if (Request.IsAjaxRequest())
                return PartialView("FeedBackList", result);
            return View(result);
        }

        // GET: /FeedBack/Details/5
        public ActionResult Details(int id)
        {
            Feedback result = accountBLL.GetFeedback(new Feedback { Id = id }).FirstOrDefault();
            return View(result);
        }

        // GET: /FeedBack/Create
        public ActionResult Create()
        {
            return View();
        }

        // GET: /FeedBack/Edit/5
        public ActionResult Edit(int id)
        {
            Feedback result = accountBLL.GetFeedback(new Feedback { Id = id }).FirstOrDefault();
            return View(result);
        }

        // GET: /FeedBack/Delete/5
        public ActionResult Delete(int id)
        {
            bool isSuccessful = accountBLL.DeleteFeedBack(id);
            if (isSuccessful)
            {
                TempData["ExcuteSuccfull"] = "Delete Successfully.";
            }
            else
            {
                TempData["ExcuteSuccfull"] = "Operation fail. Please contact with the administrator.";
            }
            return RedirectToAction("Index");
        }
        #endregion

        #region Post
        // POST: /FeedBack/Create
        [HttpPost]
        public ActionResult Create(Feedback collection)
        {
            try
            {
                if (accountBLL.GetFeedback(new Feedback { FeedSubject = collection.FeedSubject }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("FeedSubject", ConstantHelper.ERROR_NAME_EXIST);
                }
                if (ViewData.ModelState.IsValid)
                {
                    accountBLL.AddFeedBack(new List<Feedback> { collection });
                    return RedirectToAction("Index");
                }
                return View();

            }
            catch
            {
                return View();
            }
        }

        // POST: /FeedBack/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, Feedback collection)
        {
            try
            {
                if (accountBLL.GetFeedback(new Feedback { FeedSubject = collection.FeedSubject }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("FeedSubject", ConstantHelper.ERROR_NAME_EXIST);
                }
                if (ViewData.ModelState.IsValid)
                {
                    accountBLL.UpdataFeedBack(new List<Feedback> { collection });
                    return RedirectToAction("Index");
                }
                return View(collection);

            }
            catch
            {
                return View();
            }
        }

        #endregion

    }
}
